/** @method 设置token */
export function setToken(token) {
	localStorage.setItem('UserToken', token);
}

/**
 * @method 获取token
 * @returns {string} 当前Token
 */
export function getToken() {
	return localStorage.getItem('UserToken');
}

/**
 * @method 删除token
 * @returns {boolean} 是否删除Token成功
 */
export function removeToken() {
	return localStorage.removeItem('UserToken');
}

/** @method 设置refreshToken */
export function setRefreshToken(token) {
	localStorage.setItem('UserRefreshToken', token);
}

/** @method 获取refreshToken  */
export function getRefreshToken() {
	return localStorage.getItem('RefreshToken');
}

/**
 * @method 去除refresh token
 * @returns {boolean} 是否删除refreshToken成功
 */
export function removeRefreshToken() {
	localStorage.removeItem('UserRefreshToken');
}

/** @method 获取用户信息 */
export function getUserInfo() {
	return {
		userId: '',
		userName: '',
		userRole: 'user',
	};
}

/** @method 去除用户相关缓存 */
export function clearAuthStorage() {
	removeToken();
	removeRefreshToken();
}

/** @method 获取用户相关搜索缓存 */
export function getSearchHistory() {
	return JSON.parse(localStorage.getItem('SearchHistory'));
}

/** @method 写入用户相关搜索缓存 */
export function setSearchHistory(value) {
	let SearchHistory = getSearchHistory();
	if (SearchHistory) {
		SearchHistory.find((item) => item === value) ? '' : SearchHistory.push(value);
	} else {
		SearchHistory = [value];
	}

	localStorage.setItem('SearchHistory', JSON.stringify(SearchHistory));
}

/** @method 删除搜索历史 */
export function removeSearchHistory(value) {
	let SearchHistory = getSearchHistory();
	if (SearchHistory) {
		SearchHistory = SearchHistory.filter((item) => item != value);
		localStorage.setItem('SearchHistory', JSON.stringify(SearchHistory));
	}
}

/** @method 清除用户相关搜索缓存 */
export function clearSearchStorage() {
	localStorage.removeItem('searchHistory');
}
